﻿/*功能描述：客户设置
作者：李周蓬
时间：2021-12-07
修改人：李周蓬
修改时间：2021-12-07*/
using HongStone.Domain.ClientManagementModel;
using HongStone.Dto;
using HongStone.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using MiniExcelLibs;
using System.Linq;
using System.Threading.Tasks;

namespace HongStone.API.Controllers
{
    [ApiExplorerSettings(GroupName = "Customer")]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CustomerController : ControllerBase
    {
        private readonly CustomerBll _customerBll;
        public CustomerController(CustomerBll customerBll)
        {
            _customerBll = customerBll;
        }
        /// <summary>
        /// 添加甲方负责人
        /// </summary>
        /// <param name="responsiblePerson"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddResponsiblePerson(ResponsiblePerson responsiblePerson)
        {
            return Ok(_customerBll.AddResponsiblePerson(responsiblePerson));
        }
        /// <summary>
        /// 添加满意度调查
        /// </summary>
        /// <param name="satisfactionSurvey"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddSatisfactionSurvey(SatisfactionSurvey satisfactionSurvey)
        {
            return Ok(_customerBll.AddSatisfactionSurvey(satisfactionSurvey));
        }
        
        //[HttpGet]
        //public IActionResult ShowManYiDu(string manyidu)
        //{
        //    return Ok(_customerBll.ShowManYiDu(manyidu));
        //}
        /// <summary>
        /// 显示满意度
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowManYiDu()
        {
            return Ok(_customerBll.ShowManYiDu());
        }
        [HttpGet]
        public IActionResult BindCity(int id)
        {
            return Ok(_customerBll.BindCity(id));
        }
        [HttpPost]
        public IActionResult AddEntering(Customer customer)
        {
            return Ok(_customerBll.AddEntering(customer));
        }
        [HttpPost]
        public IActionResult AddEnterings(ExtendInformation extendInformation)
        {
            return Ok(_customerBll.AddEnterings(extendInformation));
        }
        /// <summary>
        /// 添加附件
        /// </summary>
        /// <param name="accessory"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddFile(Accessory accessory)
        {
            return Ok(_customerBll.AddFile(accessory));
        }
        [HttpPost]
        public IActionResult ShowList(CheckDto checkDto, int pageindex=1, int pagesize=3)
        {
            var totalcount = 0;
            var list = _customerBll.ShowList(pageindex, pagesize, checkDto,  out totalcount);
            return Ok(new {totalcount,list });
        }
        /// <summary>
        /// 显示列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowLie()
        {
            return Ok(_customerBll.ShowLie());
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Del(string ids)
        {
            return Ok(_customerBll.Del(ids));
        }
        /// <summary>
        /// 批量删除客户扩展信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Dels(string ids)
        {
            return Ok(_customerBll.Dels(ids));
        }
        /// <summary>
        /// 显示甲方负责人
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowJia()
        {
            return Ok(_customerBll.ShowJia());
        }
        /// <summary>
        /// 显示满意度调查
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowMan()
        {
            return Ok(_customerBll.ShowMan());
        }
        //[HttpPost]
        //public IActionResult DownloadExcel()
        //{
        //    var values = _customerBll.ShowList(checkDto, 1, 1000);
        //    var memoryStream = new MemoryStream();
        //    memoryStream.SaveAs(values.ToList());
        //    memoryStream.Seek(0, SeekOrigin.Begin);
        //    return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
        //    {
        //        FileDownloadName = "demo.xlsx"
        //    };
        //}
        /// <summary>
        /// 修改客户基本信息
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateCustomer(Customer customer)
        {
            return Ok(_customerBll.UpdateCustomer(customer));
        }
        /// <summary>
        /// 修改客户扩展信息
        /// </summary>
        /// <param name="extendInformation"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpdateExtendInformation(ExtendInformation extendInformation)
        {
            return Ok(_customerBll.UpdateExtendInformation(extendInformation));
        }
        /// <summary>
        /// 删除甲方负责人
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DelJia(string id)
        {
            return Ok(_customerBll.DelJia(id));
        }
        /// <summary>
        /// 删除满意度调查
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DelMan(string id)
        {
            return Ok(_customerBll.DelMan(id));
        }
        /// <summary>
        /// 修改甲方负责人
        /// </summary>
        /// <param name="responsiblePerson"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateJia(ResponsiblePerson responsiblePerson)
        {
            return Ok(_customerBll.UpdateJia(responsiblePerson));
        }
        /// <summary>
        /// 修改满意度调查
        /// </summary>
        /// <param name="satisfactionSurvey"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateMan(SatisfactionSurvey satisfactionSurvey)
        {
            return Ok(_customerBll.UpdateMan(satisfactionSurvey));
        }
        /// <summary>
        /// 联系人信息的显示查询分页
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="name"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowJiaPage(string name, DateTime? beginTime, DateTime? endTime, int pageindex=1, int pagesize=3)
        {
            var totalcount = 0;
            var list = _customerBll.ShowJiaPage(pageindex, pagesize, name, beginTime, endTime,out totalcount);
            return Ok(new { list,totalcount});
        }
        /// <summary>
        /// 显示分页查询文件
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="name"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="totalcount"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowFile(string name, DateTime? beginTime, DateTime? endTime,int pageindex=1, int pagesize=3)
        {
            var totalcount = 0;
            var list = _customerBll.ShowFile(name, beginTime, endTime, out totalcount, pageindex, pagesize);
            return Ok(new { list, totalcount });
        }
        /// <summary>
        /// 客户高级查询查询
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="number"></param>
        /// <param name="name"></param>
        /// <param name="jian"></param>
        /// <param name="addr"></param>
        /// <param name="lian"></param>
        /// <param name="phone"></param>
        /// <param name="sheng"></param>
        /// <param name="shi"></param>
        /// <param name="xian"></param>
        /// <param name="type"></param>
        /// <param name="hang"></param>
        /// <param name="time"></param>
        /// <param name="totalcount"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowCustomerTotal( string number, string name, string jian, string addr, string lian, string phone, string sheng, string shi, int type, int hang, DateTime? begintime, DateTime? endtime, int pageindex = 1, int pagesize = 3)
        {
            var totalcount = 0;
            var list= _customerBll.ShowCustomerTotal(pageindex, pagesize, number, name, jian, addr, lian, phone, sheng, shi, type, hang, begintime,endtime, out totalcount);
            return Ok(new { list, totalcount });
        }
        /// <summary>
        /// 显示文件
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowFiles()
        {
            return Ok(_customerBll.ShowFiles());
        }
        /// <summary>
        /// 批量删除文件
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DelProject(string ids)
        {
            return Ok(_customerBll.DelProject(ids));
        }
        /// <summary>
        /// 显示录入列表
        /// </summary>
        /// <param name="checkDto"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="totalcount"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ShowLists(int pageindex, int pagesize)
        {
            var totalcount = 0;
            var list = _customerBll.ShowLists(pageindex, pagesize, out totalcount);
            return Ok(new { list, totalcount });
        }
        /// <summary>
        /// 导出客户列表页面的数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownloadExcel()
        {
            var totalcount = 0;
            var values= _customerBll.ShowLists(1, 10000, out totalcount);
            var zhuan = new List<Dictionary<string, string>>();
            List<CustomerDto> data = (List<CustomerDto>)values.Data;
            foreach (var item in data)
            {
                zhuan.Add(new Dictionary<string, string>
                {
                    { "序号",$"{item.CustomerId}" },{ "客户简称",$"{item.CustomerReferred}" },{ "客户名称",$"{item.CustomerName}" },{ "公司地址",$"{item.CompanyAddress}" },
                    { "邮政编码",$"{item.PostalCode}" },{ "联系人",$"{item.Linkman}" },{ "公司电话",$"{item.CompanyTel}" },{ "传真号",$"{item.FaxNumber}" },
                    { "客户编号",$"{item.CustomerSerialNumber}" },{ "客户英文名",$"{item.EnglishName}" },{ "是否合作",$"{item.CooperationName}" },{ "所在国家",$"{item.CountryName}" },
                    { "所在省份",$"{item.ProvinceName}" },{ "所在城市",$"{item.CityName}" },{ "客户类型",$"{item.TypeName}" },{ "所属行业",$"{item.IndustyName}" },
                    { "分支机构",$"{item.Embranchment}" },{ "Email",$"{item.Email}" },{ "公司主页",$"{item.HomePage}" },{ "关系建立时间",$"{item.RelationTime}" },
                    { "关系部门",$"{item.RelationDepartment}" },{ "信用级别",$"{item.CreditRatingsName}" },{ "亲密度",$"{item.IntimacyName}" },{ "开户银行名称",$"{item.BankName}" },
                    { "开户银行账号",$"{item.BankAccount}" },{ "企业代码",$"{item.EnterpriseCode}" },{ "法定代表",$"{item.Member}" },{ "纳税人识别号",$"{item.TaxpayerCode}" },{ "备注",$"{item.Remark}" },
                });
            }
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(zhuan);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "客户列表.xlsx"
            };
        }
        /// <summary>
        /// 导出联系人页面的数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownloadExcelLian()
        {

            var totalcount = 0;
            var values = _customerBll.ShowJiaPage(1, 10000, "",  null, null,out totalcount);
            var zhuans = new List<Dictionary<string, string>>();
            List<ResponsiblePersonDto> data = (List<ResponsiblePersonDto>)values.Data;
            foreach (var item in data)
            {
                zhuans.Add(new Dictionary<string, string>
                {
                    { "编号",$"{item.SerialNumber}" },{ "姓名",$"{item.PersonName}" },{ "职务",$"{item.PersonJob}" },{ "部门",$"{item.PersonDepartment}" },
                    { "电话",$"{item.PersonPhone}" },{ "Email",$"{item.PersonEmail}" },{ "备注",$"{item.PersonRemark}" },{ "商务电话",$"{item.PersonBusinessCall}" },
                    { "住宅电话",$"{item.PersonHomePhone}" },{ "商务传真",$"{item.PersonBusinessFax}" },{ "住宅传真",$"{item.PersonResidentialFax}" },{ "录入时间",$"{item.PersonTime}" }
                });
            }
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(zhuans);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "联系人.xlsx"
            };
        }
        /// <summary>
        /// 导出项目前期数据页面的数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownloadExcelFile()
        {

            var totalcount = 0;
            var values = _customerBll.ShowFile("", null, null, out totalcount, 1, 10000);
            var zhuan = new List<Dictionary<string, string>>();
            List<AccessoryDto> data = (List<AccessoryDto>)values.Data;
            foreach (var item in data)
            {
                zhuan.Add(new Dictionary<string, string>
                {

                    { "序号",$"{item.CustomerId}" },
                    { "客户编号",$"{item.CustomerSerialNumber}" },{ "客户英文名",$"{item.EnglishName}" },{ "是否合作",$"{item.CooperationName}" },{ "所在国家",$"{item.CountryName}" },
                    { "所在省份",$"{item.ProvinceName}" },{ "所在城市",$"{item.CityName}" },{ "客户类型",$"{item.TypeName}" },{ "所属行业",$"{item.IndustyName}" },
                    { "分支机构",$"{item.Embranchment}" },{ "Email",$"{item.Email}" },{ "公司主页",$"{item.HomePage}" },{ "关系建立时间",$"{item.RelationTime}" },
                    { "关系部门",$"{item.RelationDepartment}" },{ "信用级别",$"{item.CreditRatingsName}" },{ "亲密度",$"{item.IntimacyName}" },{ "开户银行名称",$"{item.BankName}" },
                    { "开户银行账号",$"{item.BankAccount}" },{ "企业代码",$"{item.EnterpriseCode}" },{ "法定代表",$"{item.Member}" },{ "纳税人识别号",$"{item.TaxpayerCode}" },{ "备注",$"{item.Remark}" },
                    { "文件编号",$"{item.FileId}" },{ "文件名",$"{item.FileName}" },{ "上传时间",$"{item.UploadTime}" },{ "文件大小",$"{item.FileSize}" },
                    { "文件类型",$"{item.FileType}" },{ "录入时间",$"{item.FileTime}" }
                });
            }
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(zhuan);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "附件.xlsx"
            };
        }
        /// <summary>
        /// 导出高级查询页面的数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownloadExcelCustomer()
        {

            var totalcount = 0;
            var values = _customerBll.ShowCustomerTotal(1, 10000, "", "", "", "", "", "", "", "", 0, 0, null,null, out totalcount);
            var zhuan = new List<Dictionary<string, string>>();
            List<CustomerDto> data = (List<CustomerDto>)values.Data;
            foreach (var item in data)
            {
                zhuan.Add(new Dictionary<string, string>
                {
                    { "序号",$"{item.CustomerId}" },{ "客户简称",$"{item.CustomerReferred}" },{ "客户名称",$"{item.CustomerName}" },{ "公司地址",$"{item.CompanyAddress}" },
                    { "邮政编码",$"{item.PostalCode}" },{ "联系人",$"{item.Linkman}" },{ "公司电话",$"{item.CompanyTel}" },{ "传真号",$"{item.FaxNumber}" },
                    { "客户编号",$"{item.CustomerSerialNumber}" },{ "客户英文名",$"{item.EnglishName}" },{ "是否合作",$"{item.CooperationName}" },{ "所在国家",$"{item.CountryName}" },
                    { "所在省份",$"{item.ProvinceName}" },{ "所在城市",$"{item.CityName}" },{ "客户类型",$"{item.TypeName}" },{ "所属行业",$"{item.IndustyName}" },
                    { "分支机构",$"{item.Embranchment}" },{ "Email",$"{item.Email}" },{ "公司主页",$"{item.HomePage}" },{ "关系建立时间",$"{item.RelationTime}" },
                    { "关系部门",$"{item.RelationDepartment}" },{ "信用级别",$"{item.CreditRatingsName}" },{ "亲密度",$"{item.IntimacyName}" },{ "开户银行名称",$"{item.BankName}" },
                    { "开户银行账号",$"{item.BankAccount}" },{ "企业代码",$"{item.EnterpriseCode}" },{ "法定代表",$"{item.Member}" },{ "纳税人识别号",$"{item.TaxpayerCode}" },{ "备注",$"{item.Remark}" },
                });
            }
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(zhuan);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "高级查询.xlsx"
            };
        }
        /// <summary>
        /// 导出查看页面的数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult DownloadExcelCheck()
        {

            var totalcount = 0;
            var values = _customerBll.ShowCustomerTotal(1, 10000, "", "", "", "", "", "", "", "", 0, 0, null, null, out totalcount);
            var zhuan = new List<Dictionary<string, string>>();
            List<CustomerDto> data = (List<CustomerDto>)values.Data;
            foreach (var item in data)
            {
                zhuan.Add(new Dictionary<string, string>
                {
                    { "序号",$"{item.CustomerId}" },{ "客户简称",$"{item.CustomerReferred}" },{ "客户名称",$"{item.CustomerName}" },{ "公司地址",$"{item.CompanyAddress}" },
                    { "邮政编码",$"{item.PostalCode}" },{ "联系人",$"{item.Linkman}" },{ "公司电话",$"{item.CompanyTel}" },{ "传真号",$"{item.FaxNumber}" },
                    { "客户编号",$"{item.CustomerSerialNumber}" },{ "客户英文名",$"{item.EnglishName}" },{ "是否合作",$"{item.CooperationName}" },{ "所在国家",$"{item.CountryName}" },
                    { "所在省份",$"{item.ProvinceName}" },{ "所在城市",$"{item.CityName}" },{ "客户类型",$"{item.TypeName}" },{ "所属行业",$"{item.IndustyName}" },
                    { "分支机构",$"{item.Embranchment}" },{ "Email",$"{item.Email}" },{ "公司主页",$"{item.HomePage}" },{ "关系建立时间",$"{item.RelationTime}" },
                    { "关系部门",$"{item.RelationDepartment}" },{ "信用级别",$"{item.CreditRatingsName}" },{ "亲密度",$"{item.IntimacyName}" },{ "开户银行名称",$"{item.BankName}" },
                    { "开户银行账号",$"{item.BankAccount}" },{ "企业代码",$"{item.EnterpriseCode}" },{ "法定代表",$"{item.Member}" },{ "纳税人识别号",$"{item.TaxpayerCode}" },{ "备注",$"{item.Remark}" },
                });
            }
            var memoryStream = new MemoryStream();
            memoryStream.SaveAs(zhuan);
            memoryStream.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "客户查询.xlsx"
            };
        }

        [HttpGet]
        public IActionResult ShowCheckList()
        {
            return Ok(_customerBll.ShowCheckList());
        }
        [HttpGet]
        public IActionResult ShowCheckListCheck()
        {
            return Ok(_customerBll.ShowCheckListCheck());
        }
    }
}
